<?php
/**
 * DatabaseServiceProvider
 *
 * PHP version 7
 *
 * @category  DatabaseServiceProvider
 * @package   App\Providers
 * @author    Guojing Liu <liugj@boqii.com>
 * @copyright 2016-2019 guangcheng Co. All Rights Reserved.
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
 * @version   GIT:<git_id>
 * @link      http://shop.openapi.boqii.com
 */

namespace App\Providers;

use Event;
use Illuminate\Support\ServiceProvider;
use Illuminate\Database\Events\StatementPrepared;

class DatabaseServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
		Event::listen(StatementPrepared::class, function ($event) {
            $event->statement->setFetchMode(\PDO::FETCH_ASSOC);
        });
        app('db')->listen(
            function ($query) {
                $prepareSql = str_replace(["?","\r\n", "\r", "\n"], ["'%s'", '', '', ''], $query->sql);
                $prepareSql = preg_replace('/:[0-9a-z_]+/i', "'%s'", $prepareSql);
                $sql = vsprintf($prepareSql, $query->bindings);
                $this->app['log']->info(sprintf('sql:%s cost:%dms', $sql, $query->time));
            }
        );
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
